# -*- coding: utf-8 -*-
'''
Created on 2018年8月1日
@author: Administrator
'''
#1.高级操作(文件上传)
import requests
files = {'file':open('favicon.icon','rb')}
response = requests.post('http://httpbin.org/post',files=files)
print(response.text)

#2.获取cookie
#直接使用.cookies获取网站的cookies
import requests
response = requests.get("https://www.baidu.com")
print(response.cookies)
for key,value in response.cookies.items():
    print(key+'='+value)
    
#3(1).会话维持1
#常用于模拟登录,这里获取的cookies结果为空，是因为进行了两次get请求，这两次get请求是相互独立的
#所以获取到的cookies为空
import requests
requests.get('http://httpbin.org/cookies/set/number/123456789')
response = requests.get('http://httpbin.org/cookies')
print(response.text)
#3(2).会话维持2
#利用session方法能够会话维持
import requests
s = requests.session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)

#4(1).证书验证
#证书不是官方认证的，直接get请求抛出SSLError;
import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)
#4(2).证书验证
#加上verify=False就不会抛出SSLError了，但是会抛出警告信息，提醒你最好检验证书
#如果要清除警告信息，可以像（3）那样写
import requests
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
#4(3).证书验证
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
#4(4).证书验证
#可以手动指定一证书,因本地没有，就仅只有代码
import requests
response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
print(response.status_code)